In [1]:
def __str__(self):
        return unicode(self).encode('utf-8')
import time

Problem 1.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.


In [2]:
x = range(1,10)
x


Out[2]:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]:
def sum_number_mode(a,b,maxbarrier):
    x = 0
    for i in range(1,maxbarrier-1):
        if i%a == 0 or i%b == 0:
            x += i
    return x
        
    
sum_number_mode(3,5,1001)


Out[3]:
233168

Problem 2.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


In [4]:
x = [3,4]
x[1]


Out[4]:
4

In [6]:
def sum_even_fibonacci(mod, maxfibonacci):
    x = [1,2] 
    sum_fib = 0
    
          
    while x[1] < maxfibonacci: 
        if x[1]%mod == 0:
            sum_fib += x[1]
        y = x[1]
        x[1] += x[0]
        x[0] = y  
        
    return sum_fib
        
sum_even_fibonacci(2,40000000000)


Out[6]:
26658145586

Problem 3.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?


In [7]:
def greatest_factor(prime):
    x = 2  # initiate first factor
    while x != prime:
        if prime%x == 0:
            result = prime/x
            prime = result
            
        else:
            x += 1
    else:
        return prime

greatest_factor(81)


Out[7]:
3

Problem 4.

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.


In [8]:
import time
start_time = time.time()

def palindrome(number):
    return str(number) == str(number)[::-1]

list_number = []
for x in range(800,1000):
    for i in range(800,1000):
        result = i*x
        if palindrome(result) == True: 
            list_number  += [result,]
max(list_number)
print("--- %s seconds ---" % (time.time() - start_time))


--- 0.0698199272156 seconds ---

In [9]:
start_time = time.time()

list_number = []
for x in range(800,1000):
    for i in range(800,1000):
        result = i*x
        s = list(__str__(result))
        if (s[0] == s[5]) and (s[1] == s[4]) and (s[2] == s[3]): 
            list_number  += [result,]
max(list_number)
print("--- %s seconds ---" % (time.time() - start_time))


--- 0.161638021469 seconds ---

Problem 5.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?


In [10]:
def smallest_divided(start, max_number):
    factors = range(start, max_number + 1)
    x = (len(factors)) - 1
    factors_result = factors[0]
    while (factors[x]-1) >= factors[0]:
        if factors_result%factors[x-1] !=0 :
            factors_result *= factors[x-1]
            print factors_result
        x -= 1
    #return factors_result
    
           
smallest_divided(1,20)


19
342
5814
93024
1395360
19535040
253955520
2793510720

In [14]:
def factoring(prime):
    x = 2  # initiate first factor
    factors = []
    result = 0
    while x != prime:
        while (prime%x == 0):
            result = prime/x
            prime = result
            factors.append(x)
            if prime == 1:
                break 
        if prime == 1:
                break 
        else:
            x += 1
    else:
        factors.append(prime)
    return factors

factoring(20)


Out[14]:
[2, 2, 5]

In [26]:
def smallest_divided(start, max_number):
    x = range(start, max_number + 1)
    factors = []
    for i in x:
        z = factoring(i)
        factors.append(z)
    return factors    

smallest_divided(1,20)
faktor = smallest_divided(1,20)

In [49]:
set_factor = faktor
def add_set(set_factor):
    set_all = []
    intersect = []
    join = []
    for i in set_factor:
        intersect = i and set_all
        i = i - intersect
        set_all = i + set_all
        
    return set_all

In [61]:
set_all_simulate = []
for i in set_factor:
    set_all_simulate += i
       
print set_all_simulate

factor_set = set(set_all_simulate)
factor_set
z = 1
for i in factor_set:
    z *= i
print z


[2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 2, 3, 2, 3, 2, 3, 2, 3, 7, 7, 7, 7, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 5, 2, 5, 2, 5, 2, 5, 11, 11, 11, 11, 2, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 3, 13, 13, 13, 13, 2, 7, 2, 7, 2, 7, 2, 7, 3, 5, 3, 5, 3, 5, 3, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 17, 17, 17, 17, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 19, 19, 19, 19, 2, 2, 5, 2, 2, 5, 2, 2, 5, 2, 2, 5]
9699690

In [51]:
set_factor


Out[51]:
[[],
 [2, 2],
 [3, 3],
 [2, 2, 2, 2],
 [5, 5],
 [2, 3, 2, 3],
 [7, 7],
 [2, 2, 2, 2, 2, 2],
 [3, 3, 3, 3],
 [2, 5, 2, 5],
 [11, 11],
 [2, 2, 3, 2, 2, 3],
 [13, 13],
 [2, 7, 2, 7],
 [3, 5, 3, 5],
 [2, 2, 2, 2, 2, 2, 2, 2],
 [17, 17],
 [2, 3, 3, 2, 3, 3],
 [19, 19],
 [2, 2, 5, 2, 2, 5]]

Problem 6.

The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + ... + 10^2 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


In [82]:
def sum_of_squares(start, end):
    sum_all = 0
    for i in range(start, end +1):
        sum_all += (i)**2
    return sum_all
su_sq = sum_of_squares(1,100)

In [81]:
def square_of_sum(start, end):
    sum_all_i = 0
    b = 0
    for i in range(start, end + 1):
        sum_all_i += i
    b = sum_all_i * sum_all_i
    return b

sq_su = square_of_sum(1,100)

In [83]:
sq_su - su_sq


Out[83]:
25164150

In [ ]: